﻿using System.Web.Mvc;
using System.Web.UI;
using System.Collections.Generic;
using Liber;
using Liber.WEB;

namespace SoBusiness.Controllers
{
    public class SaleController : LiberController
    {
        // GET: Sale
        [AppAurhority("Sale", "Order", "Admin")]
        public ViewResult Orders()
        {
            Response.Cache.SetOmitVaryStar(true);
            return View();
        }

        public ViewResult MyOrders()
        {
            return View();
        }

        public ViewResult Quotation()
        {
            Response.Cache.SetOmitVaryStar(true);
            return View();
        }

        public ViewResult Delivery()
        {
            return View();
        }
        public ViewResult WaitDelivery()
        {
            return View();
        }

        [AppAurhority("Sale", "Delivery", "Create")]
        public ViewResult DeliveryBillEdit()
        {
            return View();
        }


        /// <summary>
        /// 微信端筛选我的新销售订单
        /// </summary>
        /// <param name="openuid"></param>
        /// <param name="oid"></param>
        /// <returns></returns>
        [NoSession]
        [WxSession]
        public ViewResult WxMyOrders()
        {
            return View();
        }

        [AppAurhority("Sale","Order","Confirm")]
        public JsonResult ConfirmOrder(long oid = -1)
        {
            if (oid != -1)
            {
                var r = API.Request("SaleApp@ConfirmOrder")
                               .Set("orderid", oid)
                               .WaitResult();
                if (r.IsPositive)
                {
                    //TODO:发送微信通知消息
                    TLB2BController.CheckMessage();
                }
                return Json(r);
            }
            else
            {
                return Json(FnResult.Failure("Sale","RequiredArgs").SetError("oid","oid is empty"));
            }

        }

        [AppAurhority("Sale","Delivery","Create")]
        [HttpPost]
        public JsonResult NewDelivery(string dlvMX)
        {
            var mx = XMLSetting.LoadFromStr(dlvMX);
            var its = mx.All("item");
            List<DyObject> itms = new List<DyObject>();
            for(var i = 0; i < its.Length; i++)
            {
                var itm = new DyObject();
                itm.Set("ordermxid", its[i].First("ordermxid").Value)
                   .Set("todelivery", its[i].First("todelivery").Value);
                itms.Add(itm);
            }

            DyObject dlvdata = new DyObject();
            dlvdata.Set("arrivaltime", mx.First("deliveryOrder").First("arrivaltime").Value)
                    .Set("dlvorderno", mx.First("deliveryOrder").First("dlvorderno").Value)
                    .Set("dlvdeliverier", mx.First("deliveryOrder").First("dlvdeliverier").Value)
                    .Set("dlvmemo", mx.First("deliveryOrder").First("dlvmemo").Value)
                    .Set("orderid", mx.First("deliveryOrder").First("orderid").Value);

            dlvdata.Set("items", itms);

            var rsult = API.Request("SaleApp@NewDelivery")
                         .Set("dlvdata", dlvdata)
                         .WaitResult();
            if (rsult.IsPositive)
            {
                TLB2BController.CheckMessage();
            }
            return Json(rsult);
        }

        [AppAurhority("Sale", "Delivery", "Create")]
        [HttpPost]
        public  JsonResult AbortDelivery(long objid)
        {
            return Json(API.Request("SaleApp@AbortDelivery")
                           .Set("dlvid", objid)
                           .WaitResult());
        }

        public JsonResult QueryOrders()
        {
            bool onlymine = Request.QueryString.TryGetBoolean("onlymine", false);
            string status = Request.QueryString.TryGet("status", "all");
            string flag = Request.QueryString.TryGet("flag", "");
            string ordserial = Request.QueryString.TryGet("ordserial", "");
            string ordDate = Request.QueryString.TryGet("ordDate", "all");
            string dlvDate = Request.QueryString.TryGet("dlvDate", "all");
            string ordDateStart = Request.QueryString.TryGet("ordDateStart", "");
            string ordDateEnd = Request.QueryString.TryGet("ordDateEnd", "");
            string dlvDateStart = Request.QueryString.TryGet("dlvDateStart", "");
            string dlvDateEnd = Request.QueryString.TryGet("dlvDateEnd", "");
            string clnt = Request.QueryString.TryGet("client", "");
            string buyer = Request.QueryString.TryGet("buyer", "");

            long org = Request.QueryString.TryGetLong("org", -1);
            int page = Request.QueryString.TryGetInt("page", -1);
            int num = Request.QueryString.TryGetInt("num", 0);
            string desc = Request.QueryString.TryGet("desc", "");
            string asc = Request.QueryString.TryGet("asc", "");
            var rtn = API.Request("SaleApp@QueryOrder")
                         .Set("onlymine", onlymine)
                         .Set("status", status)
                         .Set("flag", flag)
                         .Set("ordserial",ordserial)
                         .Set("ordDate", ordDate)
                         .Set("dlvDate", dlvDate)
                         .Set("ordDateStart",ordDateStart)
                         .Set("ordDateEnd",ordDateEnd)
                         .Set("dlvDateStart",dlvDateStart)
                         .Set("dlvDateEnd",dlvDateEnd)
                         .Set("client",clnt)
                         .Set("buyer",buyer)
                         .Set("org",org)
                         .Set("page",page)
                         .Set("num",num)
                         .Set("desc",desc)
                         .Set("asc",asc)
                         .WaitResult();
            return Json(rtn);
        }

        public JsonResult CountOrders()
        {
            bool onlymine = Request.QueryString.TryGetBoolean("onlymine", false);
            string status = Request.QueryString.TryGet("status", "all");
            string flag = Request.QueryString.TryGet("flag", "");
            string ordserial = Request.QueryString.TryGet("ordserial", "");
            string ordDate = Request.QueryString.TryGet("ordDate", "all");
            string dlvDate = Request.QueryString.TryGet("dlvDate", "all");
            string ordDateStart = Request.QueryString.TryGet("ordDateStart", "");
            string ordDateEnd = Request.QueryString.TryGet("ordDateEnd", "");
            string dlvDateStart = Request.QueryString.TryGet("dlvDateStart", "");
            string dlvDateEnd = Request.QueryString.TryGet("dlvDateEnd", "");
            string clnt = Request.QueryString.TryGet("client", "");
            string buyer = Request.QueryString.TryGet("buyer", "");

            long org = Request.QueryString.TryGetLong("org", -1);
            string desc = Request.QueryString.TryGet("desc", "");
            string asc = Request.QueryString.TryGet("asc", "");

            var rtn = API.Request("SaleApp@QueryOrder")
                         .Set("onlycount",true)
                         .Set("onlymine", onlymine)
                         .Set("status", status)
                         .Set("flag", flag)
                         .Set("ordserial", ordserial)
                         .Set("ordDate", ordDate)
                         .Set("dlvDate", dlvDate)
                         .Set("ordDateStart", ordDateStart)
                         .Set("ordDateEnd", ordDateEnd)
                         .Set("dlvDateStart", dlvDateStart)
                         .Set("dlvDateEnd", dlvDateEnd)
                         .Set("client", clnt)
                         .Set("buyer", buyer)
                         .Set("org", org)
                         .Set("desc", desc)
                         .Set("asc", asc)
                         .WaitResult();
            return Json(rtn);
        }

        public JsonResult CountDeliveries()
        {
            string status = Request.QueryString.TryGet("status", "all");
            string flag = Request.QueryString.TryGet("flag", "");
            string ordserial = Request.QueryString.TryGet("ordserial", "");
            string dlvDate = Request.QueryString.TryGet("dlvDate", "all");
            string dlvDateStart = Request.QueryString.TryGet("dlvDateStart", "");
            string dlvDateEnd = Request.QueryString.TryGet("dlvDateEnd", "");
            string clnt = Request.QueryString.TryGet("client", "");
            string creater = Request.QueryString.TryGet("creater", "");
            string desc = Request.QueryString.TryGet("desc", "");
            string asc = Request.QueryString.TryGet("asc", "");

            var rtn = API.Request("SaleApp@QueryDeliveries")
                       .Set("status", status)
                       .Set("flag", flag)
                       .Set("ordserial", ordserial)
                       .Set("dlvDate", dlvDate)
                       .Set("dlvDateStart", dlvDateStart)
                       .Set("dlvDateEnd", dlvDateEnd)
                       .Set("client", clnt)
                       .Set("creater", creater)
                       .Set("desc", desc)
                       .Set("asc", asc)
                       .Set("onlycount", true)
                       .WaitResult();
            return Json(rtn);
        }

        public JsonResult QueryDeliveries()
        {
            string status = Request.QueryString.TryGet("status", "all");
            string flag = Request.QueryString.TryGet("flag", "");
            string ordserial = Request.QueryString.TryGet("ordserial", "");
            string dlvDate = Request.QueryString.TryGet("dlvDate", "all");
            string dlvDateStart = Request.QueryString.TryGet("dlvDateStart", "");
            string dlvDateEnd = Request.QueryString.TryGet("dlvDateEnd", "");
            string clnt = Request.QueryString.TryGet("client", "");
            string creater = Request.QueryString.TryGet("creater", "");
            string desc = Request.QueryString.TryGet("desc", "");
            string asc = Request.QueryString.TryGet("asc", "");
            int page = Request.QueryString.TryGetInt("page", -1);
            int num = Request.QueryString.TryGetInt("num", 0);

            var rtn = API.Request("SaleApp@QueryDeliveries")
                       .Set("status", status)
                       .Set("flag", flag)
                       .Set("ordserial", ordserial)
                       .Set("dlvDate", dlvDate)
                       .Set("dlvDateStart", dlvDateStart)
                       .Set("dlvDateEnd", dlvDateEnd)
                       .Set("client", clnt)
                       .Set("creater", creater)
                       .Set("page", page)
                       .Set("num", num)
                       .Set("desc", desc)
                       .Set("asc", asc)
                       .WaitResult();
            return Json(rtn);
        }

        public JsonResult GetDeliveryCount()
        {
            return Json(API.Request("SaleApp@CountMyDelivery").WaitResult());
        }

    }
}